<?php

/*****
* File :    .php
* module :  
* Created:  
* Update:   
* Function: 
* Author:   Eugene Heriniaina
* Email:    hery@serasera.org
* Comment:  
*******/

//manage status


switch($task) {
	case 'orderup':
		status_order( intval( $cid[0] ), -1, $option );
		break;
	case 'orderdown':
		status_order( intval( $cid[0] ), 1, $option );
		break;
	case 'new':
		status_edit( $option );
		break;
	case 'edit':
		if($cid[0]) {
			$id = $cid[0];
		}
		status_edit( $option, $id );
		break;
	case 'save':
		status_save( $option, $id );
		break;
	case 'remove';
	case 'delete';
		status_delete( $option, intval( $cid[0] ) );
		break;
	default:
		if($id) {
			status_view( $option, $id );
		} else {
			status_list( $option );
		}
		break;
}

function status_delete(  $option, $id ) {
	global $database, $access;

	$row = new congregationStatus( $database );
	$row->load( $id );

	if(!$row->id) {
		echo "<script> alert('". _Status_Not_Found ."'); window.history.go(-1); </script>n";
		exit();
	}
	$query = "SELECT count(*) FROM #__congregation_profile WHERE status='$row->id'";
	$database->setQuery( $query );

	$status = $database->loadResult();

	if($status > 0 ) {
		echo "<script> alert('". _Status_Has_Profiles ."'); window.history.go(-1); </script>n";
		exit();
	}
	$query = "DELETE FROM #__congregation_status "
	. "\n WHERE id = '$id' "
	;
	$database->setQuery( $query );
	if (!$database->query()) {
		echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>\n";
	}
	mosRedirect( "?option=$option&rub=status&Itemid=$Itemid");
	
}


function status_view( $option, $id ) {
	global $my, $access, $database;
	if(!$access->canEdit || !$my->username) {
		mosNotAuth();
		return;
	}

	$row = new congregationStatus( $database );
	$row->load( $id );


	HTML_status::status_view( $row, $option );
}

/**
* Moves the order of a record
* @param integer The increment to reorder by
*/
function status_order( $uid, $inc, $option ) {
	global $database, $option, $rub, $Itemid;

	$row = new congregationStatus( $database );
	$row->load( $uid );
	$row->move( $inc );

	mosRedirect( ("?option=$option&rub=$rub&Itemid=$Itemid") );
}

function status_list( $option ) {
	global $database;

//for listing
	$limit 		= intval( mosGetParam( $_REQUEST, 'limit', 20 ) );
	$limitstart = intval( mosGetParam( $_REQUEST, 'limitstart', 0 ) );



	$query = "SELECT count(*) FROM #__congregation_status ";

	$database->setQuery( $query );
	$total = $database->loadResult();
	
	if ( $total <= $limit ) $limitstart = 0;

	require_once( $GLOBALS['mosConfig_absolute_path'] . '/administrator/includes/pageNavigation.php' );
	$pageNav = new mosPageNav( $total, $limitstart, $limit );


	$query = "SELECT * FROM #__congregation_status ORDER BY ordering "
	. "\nLIMIT ".$pageNav->limitstart.",".$pageNav->limit
	;
	
	$database->setQuery( $query );
	$rows =  $database->loadObjectList();

	HTML_status::status_list( $rows, $option, $pageNav );

}
function status_edit( $option, $id = null) {
	global $my, $access, $database;

	$row = new congregationStatus( $database );
	$row->load( $id );

	$lists = array();

	// build the html select list for ordering
	$query = "SELECT ordering AS value, text "
	. "\n FROM #__congregation_status"
	. "\n ORDER BY ordering "
	;
	$lists['ordering'] 		= mosAdminMenus::SpecificOrdering( $row, $id, $query, 1 );

  HTML_status::status_edit( $row, $option, $lists );
}

function status_save( $option, $id ) {
	global $Itemid, $rub, $database;

	$row = new congregationStatus( $database );
	if (!$row->bind( $_POST )) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
		exit();
	}

	if (!$row->store()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
		exit();
	}

	
	mosRedirect( ("?option=$option&rub=$rub&Itemid=$Itemid"), "Saved" );


}

class HTML_status {
/**
* Status manager
*/
	function status_view( &$row, $option ) {
		global $params, $Itemid, $access;
			?>
		<table class="adminheading">
		<tr>
			<th>
			<?php echo _Status_Heading; ?>
			</th>
		</tr>
		</table>
			<?php

				if($access->canEdit) {

					?>
				<script language="JavaScript">
				<!--
					function confirmDelete() {
						var agree=confirm("<?php echo _Profile_Delete_Confirm ; ?>");
						if (agree)
							return true;
						else
							return false ;
					}
				//-->
				</script>

				<div class="adminbox">
				<?php
					echo " <a href='"  . selRelToAbs("?option=$option&rub=status&task=edit&Itemid=$Itemid&id=$row->id") . "'>" . _Congregation_Edit . "</a> ";
					echo "  <a onclick='return confirmDelete()' href='"  . selRelToAbs("?option=$option&rub=status&task=delete&Itemid=$Itemid&id=$row->id") . "'>" . _Congregation_Delete . "</a> ";
					echo ' <a href="' . ( "?option=$option&rub=casa&task=new&Itemid=$Itemid&id=$row->id" ) . '">' . _Casa_Add_New . '</a>';
				?>
				</div>
				<?php

				} 
			?>

<?php
				
	}

	function status_edit( &$row, $option, &$lists ) {
		global $params, $Itemid;
			?>
		<table class="adminheading">
		<tr>
			<th>
			<?php echo _Status_Edit; ?>
			</th>
		</tr>
		</table>
		<form action="index2.php" method="post" name="adminForm">
					<input type="hidden" name="id" value="<?php echo $row->id ;?>" />
					<input type="hidden" name="option" value="<?php echo $option ;?>" />
					<input type="hidden" name="Itemid" value="<?php echo $Itemid ;?>" />
					<input type="hidden" name="rub" value="status" />
					<input type="hidden" name="task" value="save" />
		<table class="adminform">
				<tr valign="top">
					<td><?php echo _Status_Abbr ; ?> : </td>
					<td><input type="text" name="abbr"  style="width: 300px" value="<?php echo $row->abbr ; ?>"/></td>
				</tr>
				<tr valign="top">
					<td><?php echo _Status_Name ; ?> : </td>
					<td><input type="text" name="text"  style="width: 300px" value="<?php echo $row->text ; ?>"/></td>
				</tr>
			<tr>
				<td align="left">
				<?php echo _E_ORDERING; ?>
				</td>
				<td>
				<?php echo $lists['ordering']; ?>
				</td>
			</tr>
			</table>
			</form>
	<?php

	}

	function status_list( &$rows, $option , &$pageNav ) {
		global $params, $Itemid, $access;
			?>
		<table class="adminheading">
		<tr>
			<th>
			<?php echo _Status_List; ?>
			</th>
		</tr>
		</table>
		<form action="index2.php" method="post" name="adminForm">
		<table class="adminlist">
				<tr valign="top">
					<th width="20">
					#
					</th>
					<th width="20">
					<input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count( $rows ); ?>);" />
					</th>
					<th><?php echo _Status_Abbr ; ?> </th>
				<th colspan="2" width="5%">
				<?php echo _E_ORDERING; ?>
				</th>
					<th><?php echo _Status_Name ; ?></th>
				</tr>
				<?php
				if($rows) {
				$k = 0;
				for ($i=0, $n=count( $rows ); $i < $n; $i++) {
					$row = &$rows[$i];
					$k = 1 - $k;

					$link 		= 'index2.php?option=com_congregation&rub=status&task=edit&hidemainmenu=1&id='. $row->id;

/*
					$task 	= $row->showBanner ? 'unpublish' : 'publish';
					$img 	= $row->showBanner ? 'publish_g.png' : 'publish_x.png';
					$alt 	= $row->showBanner ? 'Published' : 'Unpublished';
*/

					$checked 	= mosCommonHTML::CheckedOutProcessing( $row, $i );
					?>

			<tr class="<?php echo "row$k"; ?>">
				<td  width="20">
				<?php echo $pageNav->rowNumber( $i ); ?>
				</td>
				<td  width="20">
				<?php echo $checked; ?>
				</td>
				<td><?php echo $row->abbr ; ?> </td>
				<td align="center">
				<?php echo $pageNav->orderUpIcon( $i ); ?>
				</td>
				<td align="center">
				<?php echo $pageNav->orderDownIcon( $i, $n ); ?>
				</td>
				<td><?php echo $row->text;  ?></td>
				</tr>
						<?php

					}
				}
				?>
			</table>
		<input type="hidden" name="option" value="<?php echo $option; ?>" />
		<input type="hidden" name="rub" value="status" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="hidemainmenu" value="0" />
		<?php
		echo $pageNav->getListFooter(); 
		?>
		</form>
<?php
	
	}
	
}
?>